<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/mobile/include/taglib.jsp"%>
<html>
<head>
	<title>设置收货时间段</title>
	<%@include file="/WEB-INF/views/mobile/include/head.jsp" %>
	<script type="text/javascript"> 
		$(document).ready(function() {
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if(element.attr("class").indexOf("time")>=0){
						error.insertBefore(element.parent().parent().parent());
					}else if(element.attr("name")=="timeSlot"){
						error.appendTo(element.parent().parent());
					}else if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element.parent());
					}
				}
			});
			
			//处理
			$("#btnAdd").parent().addClass("ui-btn-right");
			
			//日期控件初始化
			fun();
			
		});
		
		var fun = function () {
			$('.time').mobiscroll().time({
		    	theme: 'android', 
				mode: 'scroller', 
				minDate: new Date(2000, 1, 1, 00, 00),
                maxDate: new Date(2050, 12, 31, 23, 59),
				display: 'modal',
				lang: 'zh'
			});
        }
		
		var delSlotIds="";
		function delTimeSlot(obj, index){
			if($(".timeslot").length == 1){
				$("#messageBox").show();
				$("#messageBox").text("时间段至少有一个。");
				return;
			}
			var slotId = $("#timeSlotId"+index).val();
			if(confirm("确认要删除该时间段吗")){
				if(slotId!=""){
					delSlotIds += ","+slotId;
				}
				if(delSlotIds.indexOf(",")==0){
					delSlotIds = delSlotIds.substring(1);
				}
				$("#delSlotIds").val(delSlotIds);
				$(obj).parent().parent().remove();
			}
		}
		
		function addTimeSlot(){
			var num = $(".addTimeSlot").length;
			var html = "<div class=\"timeslot input-small v-align addTimeSlot\">";
				html += "<div class=\"ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset\">";
				html += "<input id=\"startTime"+(num+1)+"\" data-inline=\"true\" name=\"addTimeSlots["+num+"].startTime\" type=\"text\" readonly=\"readonly\" maxlength=\"20\" class=\"time required valid\"";
				html += "value=\"\" />";
				html += "</div>";
				html += " ~ ";
				html += "<div class=\"ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset\">";
				html += "<input id=\"endTime"+(num+1)+"\" data-inline=\"true\" name=\"addTimeSlots["+num+"].endTime\" type=\"text\" readonly=\"readonly\" maxlength=\"20\" class=\"time required valid\"";
				html += "value=\"\" />";
				html += "</div>";
				html += "<input id=\"sort\" name=\"addTimeSlots["+num+"].sort\" type=\"hidden\" value=\""+(num+1)+"\" />";
				html += " <a href=\"javascript:\" data-inline=\"true\" id=\"arrow-up\" onclick=\"arrowUp(this)\" class=\"ui-link\"><i class=\"icon-arrow-up\"></i>上移</a>";
				html += " <a href=\"javascript:\" data-inline=\"true\" id=\"arrow-down\" onclick=\"arrowDown(this)\" class=\"ui-link\"><i class=\"icon-arrow-down\"></i>下移</a>";
				html += " <div class=\"ui-btn ui-input-btn ui-corner-all ui-shadow ui-btn-inline\">删除";
				html += " <input id=\"btnDel\" data-inline=\"true\" type=\"button\" value=\"删除\" onclick=\"delTimeSlot(this, "+num+")\"/>";
				html += "</div>";
				html += "</div>";
			$(".timeslots").append(html);
			fun();
		}
	</script>
</head>
<body data-role="page">
	<div data-role="header" class="ui-header" data-position="fixed" data-tap-toggle="false">
		<div data-role="controlgroup" data-type="horizontal" class="ui-btn-left">
			<a href="${ctx }/ivm/merchant/set"><img class="head-back-icon" src="${ctxFront}/images/head/android/back.png"/></a>
	    </div>
	    <h1>设置收货时间段</h1>
	    <div data-role="controlgroup" data-type="horizontal" class="ui-mini ui-btn-right">
	        <a href="javascript:" onclick="$('#inputForm').submit()" class="float-right"><img class="head-save-icon" src="${ctxFront}/images/head/android/save.png"/></a>
	    </div>
	</div>
	<div role="main" class="ui-content">
		<div id="popup" data-role="popup" data-position-to="window" data-overlay-theme="a" data-theme="b" class="ui-content">
		  <p></p>
		</div>
		<form:form id="inputForm"  modelAttribute="receiptTime"  action="${ctx}/ivm/product/receipttime/save" method="post" >
			<form:hidden path="id"/>
			<form:hidden path="merchant.id"/>
			<tags:mbmessage content="${message}"/>
			<div data-role="fieldcontain">
		          <label for="name">是否需要用户选择:</label>
					<form:radiobuttons path="needChoice" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required" />
			</div>
			<div data-role="fieldcontain">
		          <label for="assisTitle">是否提供当日送:</label>
					<form:radiobuttons path="daySend" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required" />
			</div>
			<div data-role="fieldcontain">
		          <label for="shopUrl">是否提供次日送:</label>
					<form:radiobuttons path="secondDaySend" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required" />
			</div>
			<div data-role="fieldcontain">
		          <label for="timeSlot">收货设置:</label>
		          <div class="input-small">
		          	最早收货时间在下<input data-inline="true" name="timeSlot" type="text" value="${receiptTime.timeSlot==0?1:receiptTime.timeSlot}" class="required digits">个时间段
		          </div>
			</div>
			<div data-role="fieldcontain">
		          <label for="remarks">设置时间段:</label>
					<input id="btnAdd" data-inline="true" type="button" value="新增" onclick="addTimeSlot()"/>
					<input id="delSlotIds" name="delSlotIds" value="" type="hidden"/>
					<div class="timeslots">
					<c:if test="${fn:length(receiptTime.timeSlots)==0}">
						<div class="timeslot input-small v-align">
							<input type="hidden" id="timeSlotId0" name="timeSlots[0].id" value=""/>
							<input id="startTime1" data-inline="true" name="timeSlots[0].startTime" type="text" readonly="readonly" maxlength="20" class="time required"
								value="" />
							~
							<input id="endTime1" data-inline="true" name="timeSlots[0].endTime" type="text" readonly="readonly" maxlength="20" class="time required"
								value="" />
							<input id="sort" name="timeSlots[0].sort" type="hidden" value="1" />
							<a href="javascript:" id="arrow-up" data-inline="true" onclick="arrowUp(this)"><i class="icon-arrow-up"></i>上移</a>
							<a href="javascript:" id="arrow-down" data-inline="true" onclick="arrowDown(this)"><i class="icon-arrow-down"></i>下移</a>
							<input id="btnDel" data-inline="true" type="button" value="删除" onclick="delTimeSlot(this, 0)"/>
						</div>
					</c:if>
					<c:if test="${fn:length(receiptTime.timeSlots)>0}">
						<c:forEach items="${receiptTime.timeSlots}" var="timeSlot" varStatus="vs">
							<div class="timeslot input-small v-align">
								<input type="hidden" id="timeSlotId${vs.index }" name="timeSlots[${vs.index }].id" value="${timeSlot.id}"/>
								<input id="startTime${vs.index+1 }" data-inline="true" name="timeSlots[${vs.index }].startTime" type="text" readonly="readonly" maxlength="20" class="time required"
									value="${timeSlot.startTime}" />
								~
								<input id="endTime${vs.index+1 }" data-inline="true" name="timeSlots[${vs.index }].endTime" type="text" readonly="readonly" maxlength="20" class="time required"
									value="${timeSlot.endTime}" />
								<input id="sort" name="timeSlots[${vs.index }].sort" type="hidden" value="${timeSlot.sort}" />
								<a href="javascript:" id="arrow-up" data-inline="true" onclick="arrowUp(this)"><i class="icon-arrow-up"></i>上移</a>
								<a href="javascript:" id="arrow-down" data-inline="true" onclick="arrowDown(this)"><i class="icon-arrow-down"></i>下移</a>
								<input id="btnDel" data-inline="true" type="button" value="删除" onclick="delTimeSlot(this, ${vs.index})"/>
							</div>
						</c:forEach>
					</c:if>
					</div>
			</div>
		</form:form>
	</div>
	<%@include file="/WEB-INF/views/mobile/layouts/menu.jsp" %>
</body>
</html>